﻿using System;
using System.Collections.Generic;
using iTunesArtworkApp.Core;
using iTunesArtworkApp.Core.Moles;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Pex;

namespace iTunesArtworkApp.Tests
{
    [TestClass]
    public class iTunesArtworkManagerTest
    {



        [TestMethod]
        public void iTunesArtworkManager_Checks_For_Null_ArtworkService()
        {
            // act 
            iTunesArtworkManager iTunesArtworkManager;
            iTunesArtworkManager artworkManager =
              new iTunesArtworkManager((IiTunesWrapper)null, (IArtworkService)null);

            // arrange
            artworkManager.PopulateMissingArtwork();
        }



        /// <summary>
        ///A test for PopulateMissingArtwork
        ///</summary>
        [TestMethod]
        public void CanPopulate_PinkFloydAlbumArt()
        {
            // act
            Album populatedAlbum = new Album()
            {
                Artist = "Pink Floyd",
                AlbumName = "Dark Side of the Moon"
            };
            var downloadUrl = "http://";
            var itunesService = new SIiTunesWrapper();
            itunesService.GetAlbums = delegate()
                                          {
                                              var albums  = new List<Album>();
                                              albums.Add(populatedAlbum);
                                              return albums;
                                          };
            itunesService.UpdateArtworkAlbumRef = delegate(ref Album a)
                                                      {
                                                          Assert.IsTrue(a == populatedAlbum);
                                                          a.Artwork.HasArtwork = true;          
                                                      };
            var artworkService = new SIArtworkService();
            artworkService.GetArtworkAlbumRef = delegate(ref Album a)
                                                     {
                                                         Assert.IsTrue(a == populatedAlbum);
                                                         a.Artwork = new Artwork { DownloadUrl = downloadUrl };
                                                     };

            
            // arrange
            var itunesArtworkManager = new iTunesArtworkManager(itunesService, artworkService);
            itunesArtworkManager.PopulateMissingArtwork();
            
       
            // assert
            Assert.AreEqual(downloadUrl, populatedAlbum.Artwork.DownloadUrl, "Incorrect download url.");
            Assert.IsTrue(populatedAlbum.Artwork.HasArtwork, "Album should have album artwork.");

        }
    } // end class
}
